package com.google.denta.dao.ibatis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.google.denta.dao.GroupDao;
import com.google.denta.entity.Group;

/**
 * @author Ihar_Padaliakin
 */
@SuppressWarnings("unchecked")
public final class GroupSqlMapDao extends SqlMapClientDaoSupport implements
        GroupDao {

    /** {@inheritDoc} */
    public Long create(final Group group) {
        return (Long) getSqlMapClientTemplate().insert("Group.create", group);
    }

    /** {@inheritDoc} */
    public Group read(final Long id) {
        return (Group) getSqlMapClientTemplate().queryForObject("Group.read",
                id);
    }

    /** {@inheritDoc} */
    public void update(final Group group) {
        getSqlMapClientTemplate().update("Group.update", group);
    }

    /** {@inheritDoc} */
    public void delete(final Long id) {
        getSqlMapClientTemplate().delete("Group.delete", id);
    }

    /** {@inheritDoc} */
    public List<Group> findAll() {
        return getSqlMapClientTemplate().queryForList("Group.findAll");
    }

    /** {@inheritDoc} */
    public Group findByName(final String name) {
        return (Group) getSqlMapClientTemplate().queryForObject(
                "Group.findByName", name);
    }

    /** {@inheritDoc} */
    public void createGroupRole(final Long gid, final Long rid) {
        final Map<String, Long> map = new HashMap<String, Long>();
        map.put("gid", gid);
        map.put("rid", rid);
        getSqlMapClientTemplate().insert("Group.createGroupRole", map);
    }

    /** {@inheritDoc} */
    public void addGroupUser(final Long gid, final Long uid) {
        final Map<String, Long> map = new HashMap<String, Long>();
        map.put("uid", uid);
        map.put("gid", gid);
        getSqlMapClientTemplate().insert("Group.addGroupUser", map);
    }

    /** {@inheritDoc} */
    public void deleteGroupRoles(final Long id) {
        getSqlMapClientTemplate().delete("Group.deleteGroupRoles", id);
    }

    /** {@inheritDoc} */
    public void deleteGroupUsers(final Long gid) {
        getSqlMapClientTemplate().delete("Group.deleteGroupUsers", gid);
    }

    /** {@inheritDoc} */
    public void deleteGroupUser(final Long gid, final Long uid) {
        final Map<String, Long> map = new HashMap<String, Long>();
        map.put("gid", gid);
        map.put("uid", uid);
        getSqlMapClientTemplate().delete("Group.deleteGroupUser", map);
    }

}
